﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace iriska
{
    public static class Log
    {
        private static bool _isInitialized;

        public static void Write(string message)
        {
            if (!_isInitialized)
            {
                Initialize();
            }

            Trace.Write(message);
#if DEBUG
            Console.Write(message);
#endif
        }

        public static void WriteLine(string format, params object[] args)
        {
            if (!_isInitialized)
            {
                Initialize();
            }

            string line = args == null ? format : string.Format(format, args);
            Trace.WriteLine(line);
#if DEBUG
            Console.WriteLine(line);
#endif
        }

        private static void Initialize()
        {
            var logName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            logName = Path.Combine(logName ?? string.Empty, Properties.Settings.Default.LogName);
            Trace.Listeners.Add(new LogListener(logName));
            _isInitialized = true;
#if DEBUG && TRACE
            Trace.Listeners.Add(new DefaultTraceListener());
#endif
        }
    }
}